<?php
namespace app\gacj\home;

use think\Validate;

use app\gacj\model\Article as ArticleModel;
use app\gacj\model\Category as CategoryModel;
use app\gacj\model\Setting as SettingModel;
use app\gacj\model\Slider as SliderModel;
use app\gacj\model\User as UserModel;

use GatewayClient\Gateway;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;

/**
 * 前台首页控制器
 * @package app\miniapp\admin
 */
class Api extends Common
{
    public $setting;

    /**
     * 初始化方法
     * @author 蔡伟明 <314013107@qq.com>
     */
    public function initialize()
    {
        parent::initialize();
        $this->setting = SettingModel::getInfo();
    }

    //设置
    public function setting()
    {
        jsuccess($this->setting);
    }

    //文章列表
    public function articleList(){
        $map = [];

        $keyword = input('get.keyword/s');
        if($keyword){
            $map[] = ['title', 'like', "%{$keyword}%"];
        }

        $category_id = input('get.category_id/d');
        if($category_id){
            $map[] = ['category_id', '=', $category_id];
        }

        $list = ArticleModel::getList($map, 'sort desc, public_time desc', true);

        jsuccess($list);
    }


    //文章详情
    public function article(){
        $id = input('get.id/d');
        $info = ArticleModel::getInfo($id);

        jsuccess($info);
    }

    //幻灯列表
    public function sliderList(){
        $map = [];

        $type_id = input('get.type/s');
        if($type_id){
            $map[] = ['type_id', '=', $type_id];
        }

        $list = SliderModel::getList($map, 'sort desc, id asc', false);
        jsuccess($list);
    }

    //分类列表
    public function categoryList(){
        $map = [];
        $list = CategoryModel::getList($map, 'id asc', false);
        jsuccess($list);
    }

    public function test()
    {

        // 保存数据
        if ($this->request->isPost()) {

            // 表单数据
            $data = $this->request->post();
            
            //https://www.workerman.net/doc/gateway-worker/work-with-other-frameworks.html
            $msg = $data['content'];

            // 设置GatewayWorker服务的Register服务ip和端口，请根据实际情况改成实际值(ip不能是0.0.0.0)
            Gateway::$registerAddress = '127.0.0.1:1238';

            // 向任意uid的网站页面发送数据
            $res = Gateway::sendToAll($msg);

            jsuccess('成功', $data);
        }

        return $this->fetch(); // 渲染模板
    }

    

}